package _17_函数式编程.Lambda._9_Lambda应用;

import java.util.ArrayList;
import java.util.Arrays;
import java.util.List;
import java.util.function.Consumer;
import java.util.function.Function;

public class _9_5_Lambda_Function {

    public static void main(String[] args) {

        List<String> list = Arrays.asList("1","123","12345","12345678");
        List<Integer> result;

        Function<String,Integer> function = (String str) -> str.length();

        result = filter(list, function);

        Consumer<Integer> consumer = (Integer i) -> System.out.print(i + " ");
        printByConsumer(result, consumer);
    }

    public static <T,R> List<R> filter(List<T> list, Function<T,R> function){
        List<R> result = new ArrayList<>();

        for (T t : list){
            result.add(function.apply(t));
        }
        return result;
    }

    public static <T> void printByConsumer(List<T> list, Consumer<T> consumer){
        for (T t : list){
            consumer.accept(t);
        }
    }
}
